package com.apobates.jforum2.troll.accounts.mapper;

import com.apobates.jforum2.troll.accounts.MemberStyle;
import com.apobates.jforum2.troll.accounts.entity.Member;
import com.apobates.jforum2.troll.accounts.entity.MemberStatusEnum;
import com.apobates.jforum2.troll.accounts.stub.vo.MemberVo;
import com.apobates.jforum2.troll.regular.MemberGroupEnum;
import com.apobates.jforum2.troll.regular.MemberRoleEnum;
import com.apobates.jforum2.troll.utils.core.api.OptionalEntityTransformMapper;
import java.util.Optional;
import java.util.function.Function;

/**
 *
 * @author xiaofanku
 */
public class MemberVoMapperEntity implements OptionalEntityTransformMapper<Member,MemberVo> {
    
    @Override
    public Optional<MemberVo> transform(Optional<Member> entity) {
        return entity.map(m->{
            return mapper().apply(m);
        });
    }
    
    /**
     * 转码Avatar
     * @since 20201125
     * @return
     */
    public static Function<Member,MemberVo> mapper(){
        return member -> {
            MemberVo mv = new MemberVo();
            mv.setId(member.getId());
            mv.setEntryDateTime(member.getRegisteDateTime());
            try {
                mv.setStatus(member.getStatus().getSymbol());
                mv.setStatusTitle(member.getStatus().getTitle());
            }catch (NullPointerException e){
                mv.setStatus(MemberStatusEnum.DELETE.getSymbol());//0
            }
            try {
                mv.setGroup(member.getMgroup().getSymbol());
                mv.setGroupTitle(member.getMgroup().getTitle());
            }catch (NullPointerException e){
                mv.setGroup(MemberGroupEnum.ANONY.getSymbol());
                mv.setGroupTitle(MemberGroupEnum.ANONY.getTitle());
            }
            try {
                mv.setRole(member.getMrole().getSymbol());
                mv.setRoleTitle(member.getMrole().getTitle());
            }catch (NullPointerException e){
                mv.setRole(MemberRoleEnum.NO.getSymbol());
                mv.setRoleTitle(MemberRoleEnum.NO.getTitle());
            }
            if(member.getMrole() != null && member.getMgroup() != null){
                mv.setStyle(MemberStyle.get(member.getMrole(), member.getMgroup()));
            }else{
                mv.setStyle("general");
            }
            mv.setAvatar(member.getAvatarURI());
            mv.setSignature(member.getSignature());
            mv.setNickname(member.getNickname());
            mv.setNames(member.getNames());
            //style
            //mv.setStyle(MemberVoMapperEntity.getStyle(member.getMgroup(), member.getMrole()));
            //mv.setGroupTitle(member.getMgroup().getTitle());
            //mv.setRoleTitle(member.getMrole().getTitle());
            return mv;
        };
    }
}